<?php
namespace app\modules\models;

use Yii;
use yii\base\Model;
use app\helpers\ApiCode as helperApiCode;
/**
 * 功能：基类
 * 时间：2016年10月31号
 * author:chushangming@wanda.cn
 */
class BaseModel extends Model{

    const SUC_CODE = 200;
    const ERR_CODE = 1005;
    const WANDA_CODE = 10001;

    /**
     * api统一返回格式
     */
    protected static function apiData($code, $msg, $data = null){
        return [
            'status' => (string) $code,
            'message' => $msg ? $msg : helperApiCode::trans($code,$msg) ,
            'data' => is_array($data) ? $data : null,
        ];
    }

    /*
     * 接收参数
     */
    protected static function acceptParam($pattern,$fields){
        $temp = '';
        $newData = array();
        foreach($fields as $key){
            $temp = Yii::$app->request->$pattern($key);
            if(!$temp){
                return self::apiData(helperApiCode::COMMMON_ERROR_CODE,  sprintf(helperApiCode::$messages[helperApiCode::REQUEST_PARAM_NULL], $key),null);
            }
            if($key == "mobile" && !preg_match("/^\d{11}$/",$temp)){
                return self::apiData(helperApiCode::MOBILE_ERROR, helperApiCode::$messages[helperApiCode::MOBILE_ERROR],null);
            }
            if ($key == "email" && !filter_var($temp, FILTER_VALIDATE_EMAIL)){
                return self::apiData(helperApiCode::EMAIL_ERROR,helperApiCode::$messages[helperApiCode::EMAIL_ERROR],null);
            }
            if($key == "group" && !in_array($temp, array('1','2','3'))){
                return self::apiData(helperApiCode::GROUP_NOT_EXSITS,helperApiCode::$messages[helperApiCode::GROUP_NOT_EXSITS],null);
            }
            $newData[$key] = $temp;
        }
        return self::apiData(helperApiCode::API_OK, helperApiCode::$messages[helperApiCode::API_OK],$newData);
    }



    //获取毫秒级时间戳
    public static function getMillisecond() {
        list($t1, $t2) = explode(' ', microtime());
        return (float)sprintf('%.0f',(floatval($t1)+floatval($t2))*1000);
    }

    public static function getImgUrl($imgId){
        $serverCount = 6;
        $imgUrl = "https://img".rand(1, $serverCount).".ffan.com/";
        return $imgId ? $imgUrl.$imgId : '' ;
    }

    /**
     *返回处理结果集合
     */
    public static function doresults($result){
        $response['data'] = isset($result["data"]) && !empty($result['data']) ? $result['data'] : null;
        if(array_key_exists('status', $result) && helperApiCode::API_OK == $result['status']){
            $response['message'] = isset($result["message"]) && !empty($result['message']) ? $result['message'] : helperApiCode::$messages[helperApiCode::API_OK];
            return self::apiData(helperApiCode::API_OK, $response['message'], $response['data']);
        }else{
            $response['message'] = isset($result["message"]) && !empty($result['message']) ? $result['message'] : '请求数据错误';
            return self::apiData($result['status'], $response['message'], $response['data']);
        }
    }


}